package com.example.warehouse_manager.service.impl;

import com.example.warehouse_manager.dto.AssignAuthDto;
import com.example.warehouse_manager.entity.Result;
import com.example.warehouse_manager.entity.Role;
import com.example.warehouse_manager.entity.RoleAuth;
import com.example.warehouse_manager.mapper.RoleAuthMapper;
import com.example.warehouse_manager.mapper.RoleMapper;
import com.example.warehouse_manager.page.Page;
import com.example.warehouse_manager.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
* @author Yang
* @description 针对表【role(角色表)】的数据库操作Service实现
* @createDate 2024-03-04 16:25:47
*/



@Service
//2、指定缓存的名称，数据保存到redis中的键的前缀，一般写上标注@CacheConfig注解类的全路径
@CacheConfig(cacheNames = "com.example.warehouse_manager.service.impl.RoleServiceImpl")
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private RoleAuthMapper roleAuthMapper;


    //查询所有角色
    @Override
    //3、查询方法上标注@Cacheable指定缓存的键
    @Cacheable(key = "'all:role'")
    public List<Role> queryAllRole() {
        return roleMapper.findAllRole();
    }

    @Override
    public List<Role> queryUserRoleById(Integer id) {
        return roleMapper.findUserRoleById(id);
    }

    //分页查询角色的业务
    @Override
    public Page queryRolePage(Page page, Role role) {
        //查询角色行数
        Integer roleRowCount = roleMapper.findRoleRowCount(role);
        //分页查询角色
        List<Role> rolePage = roleMapper.findRolePage(page, role);
        //组装分页信息
        page.setTotalNum(roleRowCount);
        page.setResultList(rolePage);
        return page;
    }

    //添加角色
    @CacheEvict(key = "'all:role'")
    @Override
    public Result saveRole(Role role) {
        //判断角色是否存在
        Role roleByNameOrCode = roleMapper.findRoleByNameOrCode(role.getRoleName(), role.getRoleCode());
        if (roleByNameOrCode!=null){
            return Result.err(Result.CODE_ERR_BUSINESS,"角色已存在！");
        }
        int i = roleMapper.insertRole(role);
        if (i > 0){
            return Result.ok("添加角色成功！");
        }
        return Result.err(Result.CODE_ERR_BUSINESS,"添加角色失败");
    }

    //启用或禁用角色
    @CacheEvict(key = "'all:role'")
    @Override
    public Result setRoleState(Role role) {
        int i = roleMapper.setRoleStateById(role.getRoleId(), role.getRoleState());
        if (i > 0){
            return Result.ok("角色启用/禁用成功");
        }
        return Result.err(Result.CODE_ERR_BUSINESS,"角色启用/禁用失败");
    }

    //删除角色
    @CacheEvict(key = "'all:role'")
    @Override
    public Result deleteRoleById(Integer roleId) {
        int i = roleMapper.removeRoleById(roleId);
        if (i>0){
            //删除角色权限关系
            roleAuthMapper.removeRoleAuthById(roleId);
            return Result.ok("删除角色成功");
        }
        return Result.err(Result.CODE_ERR_BUSINESS,"删除角色失败");
    }

    //查询角色分配的所有权限菜单的id的业务方法
    @Override
    public List<Integer> queryRoleAuthIds(Integer roleId) {
        return roleAuthMapper.findAuthIdsByRid(roleId);
    }

    //给角色分配权限
    @Override
    @Transactional
    public void saveRoleAuth(AssignAuthDto assignAuthDto) {
        //删除角色之前分配的所有权限
        roleAuthMapper.removeRoleAuthById(assignAuthDto.getRoleId());
        //添加新的角色权限关系
        List<Integer> authIdList = assignAuthDto.getAuthIds();
        for (Integer authId : authIdList) {
            RoleAuth roleAuth = new RoleAuth();
            roleAuth.setRoleId(assignAuthDto.getRoleId());
            roleAuth.setAuthId(authId);
            roleAuthMapper.insertRoleAuth(roleAuth);
        }
    }

    //根据角色id修改角色
    @Override
    @CacheEvict(key = "'all:role'")
    public Result setROleByRid(Role role) {
        int i = roleMapper.setDescByRid(role);
        if (i>0){
            return Result.ok("修改成功");
        }
        return Result.err(Result.CODE_ERR_BUSINESS, "修改失败");
    }


}




